package com.example.service.impl;

import com.example.constant.StatusConstant;
import com.example.context.BaseContext;
import com.example.entity.Orders;
import com.example.mapper.*;
import com.example.service.WorkspaceService;
import com.example.vo.BusinessDataVO;
import com.example.vo.InventoryOverViewVO;
import com.example.vo.ProductsOverViewVO;
import com.example.vo.OrderOverViewVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Service
@Slf4j
public class WorkspaceServiceImpl implements WorkspaceService {

    @Autowired
    private OrdersDao ordersDao;
    @Autowired
    private OrdersEmployeeDao ordersEmployeeDao;
    @Autowired
    private CustomerVipDao customerVipDao;
    @Autowired
    private ProductsDao productsDao;

    @Autowired
    private InventoryDao inventoryDao;


    /**
     * 根据时间段统计营业数据
     * @param begin
     * @param end
     * @return
     */
    public BusinessDataVO getBusinessData(LocalDateTime begin, LocalDateTime end) {
        /**
         * 营业额：当日已完成订单的总金额
         * 有效订单：当日已完成订单的数量
         * 订单完成率：有效订单数 / 总订单数
         * 平均客单价：营业额 / 有效订单数
         * 新增用户：当日新增用户的数量
         */
        Long storeId = BaseContext.getCurrentId2();
        Map map = new HashMap();
        map.put("begin",begin);
        map.put("end",end);
        map.put("storeId",storeId);

        //查询总订单数
        Integer totalOrderCount = ordersDao.countByMap(map) + ordersEmployeeDao.countByMap(map);

        map.put("status", Orders.COMPLETED);
        //营业额
        Double t1 = ordersDao.sumByMap(map) ;
        Double t2 = ordersEmployeeDao.sumByMap(map);
        Double turnover = (t1 == null?0.0:t1) + (t2 == null?0.0:t2);

        //有效订单数
        Integer validOrderCount = ordersDao.countByMap(map) + ordersEmployeeDao.countByMap(map);

        Double unitPrice = 0.0;

        Double orderCompletionRate = 0.0;
        if(totalOrderCount != 0 && validOrderCount != 0){
            //订单完成率
            orderCompletionRate = validOrderCount.doubleValue() / totalOrderCount;
            //平均客单价
            unitPrice = turnover / validOrderCount;
        }

        //新增会员数
        Integer newUsers = customerVipDao.countByMap(map);

        return BusinessDataVO.builder()
                .turnover(turnover)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .unitPrice(unitPrice)
                .newUsers(newUsers)
                .build();
    }


    /**
     * 查询订单管理数据
     *
     * @return
     */
    public OrderOverViewVO getOrderOverView() {
        Map map = new HashMap();
        map.put("begin", LocalDateTime.now().with(LocalTime.MIN));
        map.put("status", Orders.TO_BE_CONFIRMED);

        //待接单
        Integer waitingOrders = ordersDao.countByMap(map);

        //待派送
        map.put("status", Orders.CONFIRMED);
        Integer deliveredOrders = ordersDao.countByMap(map);

        //已完成
        map.put("status", Orders.COMPLETED);
        Integer completedOrders = ordersDao.countByMap(map) + ordersEmployeeDao.countByMap(map);

        //已取消
        map.put("status", Orders.CANCELLED);
        Integer cancelledOrders = ordersDao.countByMap(map) + ordersEmployeeDao.countByMap(map);

        //全部订单
        map.put("status", null);
        Integer allOrders = ordersDao.countByMap(map) + ordersEmployeeDao.countByMap(map);

        return OrderOverViewVO.builder()
                .waitingOrders(waitingOrders)
                .deliveredOrders(deliveredOrders)
                .completedOrders(completedOrders)
                .cancelledOrders(cancelledOrders)
                .allOrders(allOrders)
                .build();
    }

    /**
     * 查询菜品总览
     *
     * @return
     */
    public ProductsOverViewVO getProductOverView() {
        Map map = new HashMap();
        map.put("status", StatusConstant.ENABLE);
        Integer sold = productsDao.countByMap(map);

        map.put("status", StatusConstant.DISABLE);
        Integer discontinued = productsDao.countByMap(map);

        return ProductsOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }

    /**
     * 查询库存总览
     *
     * @return
     */
    public InventoryOverViewVO getInventoryOverView() {
        Map map = new HashMap();
        map.put("status", 1);
        map.put("storeId",BaseContext.getCurrentId2());
        //正在销售
        Integer sold = inventoryDao.countByMap(map);

        map.put("productNum",0);
        //已售完
        Integer soleOut = inventoryDao.countByMap(map);

        //过期
        map.put("status", 2);
        map.put("productNum",null);
        Integer discontinued = inventoryDao.countByMap(map);

        return InventoryOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .soldOut(soleOut)
                .build();
    }

}
